package com.cmccgz.udacs_landlord_webapp.data;


import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.FileResultBean;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

public class UpLoadFileRepository implements UploadFileDataSource {
    @Override
    public void upLoadFile(String url, final UploadFileLisenter callback) {
        //mediaId=MEDIAID&type=1
        try {
            if (url!=null&&url.startsWith("http")) {
                String mediaId = url.substring(url.indexOf("mediaId=")+"mediaId=".length(),url.indexOf("&type=1"));
                List<String> datas = new ArrayList<String>();
                datas.add(mediaId);
                callback.uploadSuccess(datas);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ApiService.upload(url).subscribe(new Subscriber<FileResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                callback.uploadError("上传失败！");
            }

            @Override
            public void onNext(FileResultBean fileResultBean) {
                if ("0".equals(fileResultBean.retcode)) {
                    String mediaId = fileResultBean.ret.mediaId;
                    List<String> datas = new ArrayList<String>();
                    datas.add(mediaId);
                    callback.uploadSuccess(datas);
                } else
                    callback.uploadError("上传失败！");
            }
        });
    }

    @Override
    public void upLoadFiles(List<String> urls, UploadFileLisenter callback) {
        upLoadMuilFiles(urls, callback);
    }

    private void upLoadMuilFiles(List<String> urls, UploadFileLisenter callback) {
        upLoadMuilFiles(urls, null, 0, callback);

    }

    private void upLoadMuilFiles(final List<String> urls, List<String> results, final int index, final UploadFileLisenter callback) {
        if (results == null) {
            results = new ArrayList<>();
        }
        if (urls==null||urls.size()==0) {
            callback.uploadSuccess(new ArrayList<String>());
            return;
        }
        final String url = urls.get(index);
        final List<String> result_ = results;
        upLoadFile(url, new UploadFileLisenter() {
            @Override
            public void uploadSuccess(List<String> medias) {
                if (medias == null || medias.size() != 1) {
                    callback.uploadError("上传失败！");
                    return;
                }
                result_.addAll(medias);
                if (result_.size() == urls.size()) {
                    callback.uploadSuccess(result_);
                    return;
                }
                int index_ = index + 1;
                upLoadMuilFiles(urls, result_, index_, callback);
            }

            @Override
            public void uploadError(String msg) {
                callback.uploadError("上传失败！");
            }
        });
    }
}
